﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.Api.Application.hqApplication.Commands.PigFarmss;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.Application.hqApplication.Commands.Ablactationss
{
    public class ShowAbleactionsHander : IRequestHandler<ShowAblactationsCommand, Result<PageResultDto<Ablactations>>>
    {
        private readonly IMapper mapper;

        private readonly IBaseRepository<Ablactations> ablactationsRepository;
        private readonly IBaseRepository<BedroomTypes> bedroomTypesRepository;

        public ShowAbleactionsHander(IMapper mapper, IBaseRepository<Ablactations> ablactationsRepository, IBaseRepository<BedroomTypes> bedroomTypesRepository)
        {
            this.mapper = mapper;
            this.ablactationsRepository = ablactationsRepository;
            this.bedroomTypesRepository = bedroomTypesRepository;
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<PageResultDto<Ablactations>>> Handle(ShowAblactationsCommand request, CancellationToken cancellationToken)
        {


            var result = new Result<PageResultDto<Ablactations>>() { Code = (int)CommonErrorCode.Search };
            var page = new PageResultDto<Ablactations>() { List = new List<Ablactations>(), PageCount = 0, TotalCount = 0 };
            var res = await (await ablactationsRepository.GetAll()).ToListAsync();
            if (!string.IsNullOrEmpty(request.AblactationPro))
            {
                res = res.Where(x => x.AblactationPro.Contains(request.AblactationPro)).ToList();
            }
            page.TotalCount = res.Count();
            page.PageCount = (int)Math.Ceiling(page.TotalCount * 1.0 / request.PageSize);
            page.List = res.OrderBy(x => x.AblactationId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.Data = page;
            result.Code = (int)CommonErrorCode.Success;
            return result;


          


        }
    }
}
